package service

import (
	"context"
	"fmt"
	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/codes"
	"go.opentelemetry.io/otel/trace"
	pb "kratos_cron_task/api/cron/v1"
	"kratos_cron_task/internal/biz"
	"kratos_cron_task/pkg/task2"
	"runtime"
)

type CronService struct {
	pb.UnimplementedCronServer
	CronBiz *biz.CronBiz

	task *task2.Task // 实际项目中没有用到task属性,只是在项目初始化时NewTask中启动了定时任务
}

func NewCronService(cronBiz *biz.CronBiz, task *task2.Task) *CronService {
	return &CronService{
		CronBiz: cronBiz,
		task:    task,
	}
}

func (s *CronService) RunCronTask1Service(ctx context.Context) error {
	ctx, span := otel.Tracer("Service").Start(ctx, "RunCronTask1Service", trace.WithSpanKind(trace.SpanKindProducer))
	defer func() {
		if err := recover(); err != nil {
			buf := make([]byte, 2048)
			runtime.Stack(buf, false)
			span.SetStatus(codes.Error, fmt.Sprintf("%v", err))
		}
		span.End()
	}()

	return s.CronBiz.RunCronTask1Biz(ctx)
}

//func (s *CronService) RunCronTask2Service(ctx context.Context) error {
//	ctx, span := otel.Tracer("Service").Start(ctx, "RunCronTask2Service", trace.WithSpanKind(trace.SpanKindProducer))
//	defer func() {
//		if err := recover(); err != nil {
//			buf := make([]byte, 2048)
//			runtime.Stack(buf, false)
//			span.SetStatus(codes.Error, fmt.Sprintf("%v", err))
//		}
//		span.End()
//	}()
//
//	return s.CronBiz.RunCronTask2Biz(ctx)
//}
